package com.nuaa.shopping.view;

import java.util.List;
import java.util.Scanner;

import com.nuaa.shopping.entity.Product;
import com.nuaa.shopping.entity.User;
import com.nuaa.shopping.exception.UserDiabledException;
import com.nuaa.shopping.exception.UserNotFoundException;
import com.nuaa.shopping.exception.UsernameExistsException;
import com.nuaa.shopping.service.ProductService;
import com.nuaa.shopping.service.UserService;
import com.nuaa.shopping.service.impl.ProductServiceImpl;
import com.nuaa.shopping.service.impl.UserServiceImpl;

public class Start {
	private Scanner input = new Scanner(System.in);
	private UserService userService = new UserServiceImpl();
	private ProductService productService = new ProductServiceImpl();
	
	public static void main(String[] args) {
		System.out.println("########################欢迎来到购物系统###########################");
		new Start().showSystemMenu();
	}
	
	/*
	 * 系统主菜单
	 */
	public void showSystemMenu(){
		System.out.println("----------------------------------------");
		System.out.println("1.登陆\t\t2.注册\t\t3.查看商品列表");
		System.out.println("----------------------------------------");
		System.out.print("请选择：");
		int choice = input.nextInt();
		switch(choice){
			case 1:
				login();
				break;
			case 2:
				register();
				break;
			case 3:
				showProductList();
				break;
		}
	}

	/*
	 * 用户登陆
	 */
	private void login() {
		User user = new User();
		System.out.print("请输入用户名：");
		user.setUsername(input.next());
		System.out.print("请输入密码：");
		user.setPassword(input.next());
		try {
			User u = userService.login(user);
			System.out.println("登陆成功，欢迎您："+u.getUsername());
			// 显示用户主菜单 
			new UserView(u).showMainMenu();
		} catch (UserNotFoundException | UserDiabledException e) {
			System.out.println("提示：登陆失败，"+e.getMessage());
			showSystemMenu(); // 返回系统主菜单
		}
	}

	/*
	 * 用户注册
	 */
	private void register() {
		User user = new User();
		System.out.print("请输入用户名：");
		user.setUsername(input.next());
		System.out.print("请输入密码：");
		String password = input.next();
		System.out.print("请输入确认密码：");
		String repassword = input.next();
		// 判断两次输入的密码是否一致，如果不一致提示重新输入
		
		user.setPassword(password);
		System.out.print("请输入电话：");
		user.setPhone(input.next());
		System.out.print("请输入地址：");
		user.setAddress(input.next());
		try {
			userService.register(user);
			System.out.println("提示：注册成功！");
		} catch (UsernameExistsException e) {
			System.out.println("提示："+e.getMessage());
		}
		showSystemMenu(); // 返回系统主菜单
	}

	/*
	 * 显示商品列表
	 */
	private void showProductList() {
		System.out.println("----------------------------------------");
		List<Product> products = productService.findAll();
		System.out.println("序号\t商品\t价格");
		System.out.println("----------------------------------------");
		for(int i=0;i<products.size();i++){
			Product p = products.get(i);
			System.out.println(i+1+"\t"+p.getName()+"\t\t"+p.getPrice());
		}
		showSystemMenu(); // 返回系统主菜单
	}
	
	
	
}
